package ilender.dispatching;

import ilender.background.*;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Schedules the background tasks of ilender.background.
 */
public class SchedulingListener implements ServletContextListener {

	private ScheduledExecutorService overDueScheduler;
	private ScheduledExecutorService updaterFromFacebookScheduler;

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		overDueScheduler = Executors.newSingleThreadScheduledExecutor();
		// set each 10 min .
		overDueScheduler.scheduleWithFixedDelay(new OverDueChecker(), 0, 10, TimeUnit.MINUTES);


		updaterFromFacebookScheduler=Executors.newSingleThreadScheduledExecutor();
		updaterFromFacebookScheduler.scheduleWithFixedDelay(new UpdaterFromFacebook(),0,10,TimeUnit.MINUTES);
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		overDueScheduler.shutdownNow() ;
		updaterFromFacebookScheduler.shutdownNow() ;
	}
}
